package com.android.ailaoshan.sound;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.SystemClock;
import android.widget.Chronometer;
import android.widget.Toast;

import com.android.ailaoshan.common.BaseApplication;

import java.io.File;
import java.io.IOException;


/**
 * 开启录音
 */
class StartSoundRecorder {

    private BaseApplication baseApplication;

    private Context mContext;
    private final String SOUND_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Sound" + File.separator;
    /**
     * 声音文件路径
     */
    private String filePath;
    private MediaRecorder mediaRecorder;
    private Chronometer timerTxt;

    StartSoundRecorder(Context context,Chronometer chronometer) {
        baseApplication = BaseApplication.getInstance();

        this.mContext = context;
        timerTxt = chronometer;
    }

    /**
     * 录音初始化
     */
    private MediaRecorder initRecorder(){
        MediaRecorder mediaRecorder = new MediaRecorder();
        // 设置麦克风
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 设置文件输出格式
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        // 设置音频文件编码
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

        return mediaRecorder;
    }

    /**
     * 创建音频文件
     */
    private String getFilePath(){
        // 创建文件夹
        File folder = new File(SOUND_PATH);
        boolean mkdirSuccess = true;
        if( !folder.exists() ){
            mkdirSuccess = folder.mkdirs();
        }

        // 创建文件
        if(mkdirSuccess){
            String xFilePath = SOUND_PATH + System.currentTimeMillis() + ".m4a";
            File soundFile = new File(xFilePath);
            if( !soundFile.exists() ){
                try {
                    soundFile.createNewFile();
                    return xFilePath;
                } catch (IOException e) {
                    e.printStackTrace();
                    return  "";
                }
            }
        }
        return "";
    }

    /**
     * 开始录音
     */
    void startRecorder(){
        mediaRecorder = initRecorder();
        if(mediaRecorder != null){
            filePath = getFilePath();
            if( !filePath.equals("") ){
                // 图片路径缓存
                baseApplication.setVoiceFile(filePath);

                mediaRecorder.setOutputFile(filePath);
                try{
                    mediaRecorder.prepare();
                    mediaRecorder.start();

                    beginTimer();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else{
                Toast.makeText(mContext,"创建音频文件失败",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(mContext,"录音初始化失败",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 停止录音
     */
    void stopRecorder(){
        if(mediaRecorder != null){
            try{
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;

                stopTimer();
            }catch (Exception e){
                e.printStackTrace();

                File tempFile = new File(filePath);
                if(tempFile.exists()){
                    tempFile.delete();
                }
            }
        }
    }

    /**
     * 开始计时
     */
    private void beginTimer(){
        if(timerTxt != null){
            timerTxt.setBase(SystemClock.elapsedRealtime());
            timerTxt.start();
        }
    }

    /**
     * 停止计时
     */
    private void stopTimer(){
        if(timerTxt != null){
            timerTxt.stop();
        }
    }

    /**
     * 计时重置
     */
    void resetTimer(){
        if(timerTxt != null){
            timerTxt.stop();
            timerTxt.setBase(SystemClock.elapsedRealtime());
        }
    }

    /**
     * 删除录音文件
     */
    void deleteFile(){
        if( !filePath.equals("") ){
            File soundFile = new File(filePath);
            if(soundFile.exists()){
                soundFile.delete();
            }else{
                Toast.makeText(mContext,"删除音频文件失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
